const Fruit = require('../models/Fruit');
const Warehouse = require('../models/Warehouse');
const { sequelize } = require('../config/database');

// 获取统计数据
const getStats = async (req, res) => {
  try {
    // 获取水果总数
    const fruitsCount = await Fruit.count();
    
    // 获取不同分类的数量
    const categoriesResult = await Fruit.findAll({
      attributes: [
        [sequelize.fn('DISTINCT', sequelize.col('category')), 'category']
      ],
      raw: true
    });
    const categoriesCount = categoriesResult.length;
    
    // 获取仓库总数（直接从 Warehouse 表查询）
    const warehousesCount = await Warehouse.count();
    
    res.json({
      success: true,
      data: {
        categories: categoriesCount,
        fruits: fruitsCount,
        warehouses: warehousesCount
      }
    });
  } catch (error) {
    console.error('Error fetching statistics:', error);
    res.status(500).json({
      success: false,
      message: 'Failed to fetch statistics',
      error: process.env.NODE_ENV === 'development' ? error.message : undefined
    });
  }
};

module.exports = {
  getStats
};

